home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch09
/
fig09_10.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
3KB
|
108 lines
1 // Fig. 9.10: cylindr2.h
2 // Definition of class Cylinder
3 #ifndef CYLINDR2_H
4 #define CYLINDR2_H
5
6 #include "circle2.h"
7
8 class Cylinder : public Circle {
9 friend ostream &operator<<( ostream &, const Cylinder & );
10
11 public:
12 // default constructor
13 Cylinder( double h = 0.0, double r = 0.0,
14 int x = 0, int y = 0 );
15
16 void setHeight( double ); // set height
17 double getHeight() const; // return height
18 double area() const; // calculate and return area
19 double volume() const; // calculate and return volume
20
21 protected:
22 double height; // height of the Cylinder
23 };
24
25 #endif
26
27
28 // Fig. 9.10: cylindr2.cpp
29 // Member and friend function definitions
30 // for class Cylinder.
31 #include <iostream.h>
32 #include <iomanip.h>
33 #include "cylindr2.h"
34
35
36
37 // Cylinder constructor calls Circle constructor
38 Cylinder::Cylinder( double h, double r, int x, int y )
39 : Circle( r, x, y ) // call base-class constructor
40 { setHeight( h ); }
41
42 // Set height of Cylinder
43 void Cylinder::setHeight( double h )
44 { height = ( h >= 0 ? h : 0 ); }
45
46 // Get height of Cylinder
47 double Cylinder::getHeight() const { return height; }
48
49 // Calculate area of Cylinder (i.e., surface area)
50 double Cylinder::area() const
51 {
52 return 2 * Circle::area() +
53 2 * 3.14159 * radius * height;
54 }
55
56 // Calculate volume of Cylinder
57 double Cylinder::volume() const
58 { return Circle::area() * height; }
59
60 // Output Cylinder dimensions
61 ostream &operator<<( ostream &output, const Cylinder &c )
62 {
63 output << static_cast< Circle >( c )
64 << "; Height = " << c.height;
65
66 return output; // enables cascaded calls
67 }
68
69
70 // Fig. 9.10: fig09_10.cpp
71 // Driver for class Cylinder
72 #include <iostream.h>
73 #include <iomanip.h>
74 #include "point2.h"
75 #include "circle2.h"
76 #include "cylindr2.h"
77
78 int main()
79 {
80 // create Cylinder object
81 Cylinder cyl( 5.7, 2.5, 12, 23 );
82
83 // use get functions to display the Cylinder
84 cout << "X coordinate is " << cyl.getX()
85 << "\nY coordinate is " << cyl.getY()
86 << "\nRadius is " << cyl.getRadius()
87 << "\nHeight is " << cyl.getHeight() << "\n\n";
88
89 // use set functions to change the Cylinder's attributes
90 cyl.setHeight( 10 );
91 cyl.setRadius( 4.25 );
92 cyl.setPoint( 2, 2 );
93 cout << "The new location, radius, and height of cyl are:\n"
94 << cyl << '\n';
95
96 // display the Cylinder as a Point
97 Point &pRef = cyl; // pRef "thinks" it is a Point
98 cout << "\nCylinder printed as a Point is: "
99 << pRef << "\n\n";
100
101 // display the Cylinder as a Circle
102 Circle &circleRef = cyl; // circleRef thinks it is a Circle
103 cout << "Cylinder printed as a Circle is:\n" << circleRef
104 << "\nArea: " << circleRef.area() << endl;
105
106 return 0;
107 }